package com.sskj.bibi.ui.fragment;


import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.android.arouter.launcher.ARouter;
import com.scwang.smartrefresh.header.MaterialHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.sskj.bibi.R;
import com.sskj.bibi.R2;
import com.sskj.bibi.component.DaggerUserDataComponent;
import com.sskj.bibi.presenter.BibiMainFragmentPresenter;
import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.rxbus2.Subscribe;
import com.sskj.common.rxbus2.ThreadMode;
import com.sskj.common.util.ClickUtil;
import com.sskj.lib.Constans;
import com.sskj.lib.RConfig;
import com.sskj.lib.RxBusCode;
import com.sskj.lib.base.BaseFragment;
import com.sskj.lib.bean.BibiCoinType;
import com.sskj.lib.bean.CoinBean;
import com.sskj.lib.bean.StockCoinBean;
import com.sskj.lib.bean.UserData;
import com.sskj.lib.model.room.UserViewModel;
import com.sskj.lib.util.AppDirectCheckUtil;
import com.sskj.lib.util.AppOuterCheckUtil;

import javax.inject.Inject;

import butterknife.BindView;

@Route(path = RConfig.BIBI_FRAGMENT_MAIN)
public class BibiMainFragment extends BaseFragment<BibiMainFragmentPresenter> {
    @BindView(R2.id.ivTopLeft)
    ImageView ivTopLeft;
    @BindView(R2.id.tvTitle)
    TextView tvTitle;
    @BindView(R2.id.ivTopRight)
    ImageView ivTopRight;
    @BindView(R2.id.pankouFrameLayout)
    FrameLayout pankouFrameLayout;
    @BindView(R2.id.tradeFrameLayout)
    FrameLayout tradeFrameLayout;
    @BindView(R2.id.entrustFrameLayout)
    FrameLayout entrustFrameLayout;
    @BindView(R2.id.tvEntrustCurrent)
    TextView tvEntrustCurrent;
    @BindView(R2.id.tvEntrustAll)
    TextView tvEntrustAll;
    @BindView(R2.id.ivEntrustAll)
    ImageView ivEntrustAll;
    @BindView(R2.id.smartRefreshLayout)
    SmartRefreshLayout smartRefreshLayout;
    @Autowired(required = true)
    String code;
    @Autowired
    String qu;
    @Inject
    UserViewModel userViewModel;
    private UserData userData;

    @Override
    protected int getLayoutId() {
        return R.layout.bibi_fragment_bibi_main;
    }

    @Override
    public BibiMainFragmentPresenter getPresenter() {
        return new BibiMainFragmentPresenter();
    }

    @Override
    public void initView() {
        RxBus.getDefault().register(this);
        ARouter.getInstance().inject(this);
        tvTitle.setText(code.replace("_", "/").toUpperCase());

        DaggerUserDataComponent.create().inject(this);
        smartRefreshLayout.setRefreshHeader(new MaterialHeader(getActivity()));
        Fragment pankouFragment = (Fragment) ARouter.getInstance()
                .build(RConfig.BIBI_FRAGMENT_PANKOU)
                .withString(Constans.CODE, code)
                .withString(Constans.QU, qu)
                .navigation();
        Fragment buyAndSellFragment = (Fragment) ARouter.getInstance()
                .build(RConfig.BIBI_FRAGMENT_BUY_AND_SELL)
                .withString(Constans.CODE, code)
                .withString(Constans.QU, qu)
                .navigation();
        Fragment entrustFragment = (Fragment) ARouter.getInstance()
                .build(RConfig.BIBI_FRAGMENT_CURRENT_ENTRUST)
                .withString(Constans.CODE, code)
                .withString(Constans.QU, qu)
                .navigation();
        FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
        fragmentTransaction.add(R.id.pankouFrameLayout, pankouFragment);
        fragmentTransaction.add(R.id.tradeFrameLayout, buyAndSellFragment);
        fragmentTransaction.add(R.id.entrustFrameLayout, entrustFragment);
        fragmentTransaction.show(buyAndSellFragment);
        fragmentTransaction.show(entrustFragment);
        fragmentTransaction.commitNow();
        ClickUtil.click(ivTopLeft, () -> {
            ARouter.getInstance()
                    .build(RConfig.BIBI_SLIDE)
                    .withString(Constans.QU, qu)
                    .navigation();
        });
        ClickUtil.click(tvEntrustAll, () -> {
            if (AppDirectCheckUtil.checkLogin(getActivity(), userData)) {
                ARouter.getInstance().build(RConfig.BIBI_All_ENTRUST).withString(Constans.QU,qu).navigation();
            }
        });
        ClickUtil.click(ivTopRight, () -> {
            ARouter.getInstance().build(RConfig.APP_MARKET)
                    .withString(Constans.CODE, code)
                    .withString(Constans.QU, qu)
                    .navigation();
        });
        smartRefreshLayout.setOnRefreshListener(refreshLayout -> {
            RxBus.getDefault().send(RxBusCode.BIBI_FRESH);
            smartRefreshLayout.finishRefresh(3);
        });
        userViewModel.getUsers().observe(this, users -> {
            if (users != null && users.size() > 0) {
                userData = users.get(0);
            } else {
                userData = null;
            }
            updateUser();

        });
    }

    /**
     * 根据用户信息更新界面
     */
    private void updateUser() {
        if (userData == null) {
            tvEntrustAll.setVisibility(View.GONE);
            ivEntrustAll.setVisibility(View.GONE);
        } else {
            tvEntrustAll.setVisibility(View.VISIBLE);
            ivEntrustAll.setVisibility(View.GONE);
        }
    }

    @Override
    public void initData() {
        mPresenter.getProduct(qu, code);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateWsCoin(CoinBean productBean) {
        if (code.equals(productBean.getCode())) {
            updateTitle(productBean);
        }
    }

    /**
     * 更换币种
     *
     * @param coinType
     */
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void updateCoinType(BibiCoinType coinType) {
        if (qu.equals(Constans.DANWEI) && coinType.getQu().equals(Constans.DANWEI)) {
            code = coinType.getCode();
            qu = coinType.getQu();
        } else if (!qu.equals(Constans.DANWEI) && !coinType.getQu().equals(Constans.DANWEI)) {
            code = coinType.getCode();
            qu = coinType.getQu();
        } else {
            return;
        }
        mPresenter.getProduct(qu, coinType.getCode());
    }

    /**
     * 更新顶部币种信息
     *
     * @param productBean
     */
    public void updateTitle(CoinBean productBean) {
        if (tvTitle != null) {
            tvTitle.setText(productBean.getCode().replace("_", "/").toUpperCase());
        }
    }
}
